module MobileDev
  module Client
    class Plist
      require 'plist'

      def convert_xml1(input, output)
        return if input.nil? || input.empty?
        return unless File.exist?(input)

        command = if output.nil? || output.empty?
                    "plutil -convert xml1 #{input}"
                  else
                    "plutil -convert xml1 #{input} -o #{output}"
                  end
        system(command)
      end

      def set_value(key, value, path)
        raise NotImplementedError
      end

      def get_value(key, path)
        raise NotImplementedError
      end

      def itms_service_plist(ipa_download_url, bundle_identifier, bundle_version, title, options = {})
        plist_hash = {
          "items" => [{
                        "assets" => [{
                                       "kind" => "software-package",
                                       "url" => ipa_download_url
                                     }],
                        "metadata" => {
                          "bundle-identifier" => bundle_identifier,
                          "bundle-version" => bundle_version,
                          "kind" => "software",
                          "title" => title
                        }
                      }]
        }
        File.write(options[:plist_path] || File.join(Dir.pwd, "#{title}.plist"), Plist::Emit.dump(plist_hash), mode: "w")
      end
    end
  end
end